import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class TestJDBC_select {
    public static void main(String[] args) throws SQLException {

        // 1. 这是构造datasource
        DataSource datasource = new MysqlDataSource(); // 这种是为了解耦
        ((MysqlDataSource) datasource).setUrl("jdbc:mysql://127.0.0.1:3306/java110_2?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource) datasource).setUser("root");
        ((MysqlDataSource) datasource).setPassword("123456");

        // 2. 创建连接
        Connection connection = datasource.getConnection();  // 建立连接

        // 3. 构造sql
        String sql ="select * from student";
        PreparedStatement statement = connection.prepareStatement(sql);

        // 4. 执行sql
        ResultSet resultSet = statement.executeQuery();

        while(resultSet.next()){
            // 获取id这一列的数值
            int id = resultSet.getInt("id");
            int sn = resultSet.getInt("sn");
            String str1 = resultSet.getString("name");
            String str2 = resultSet.getString("qq_mail");
            System.out.println("id:" + id);
            System.out.println("sn:" + sn);
            System.out.println("name:" + str1);
            System.out.println("qq_mail:" + str2);
        }
        // 释放资源
        resultSet.close();
        connection.close();

    }
}

